package cc.leap.web;

import cc.leap.helper.LoginHelper;
import cc.leap.model.AjaxResult;
import cc.leap.model.TableDataInfo;
import cc.leap.model.domain.SysPost;
import cc.leap.service.SysPostService;
import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.extern.java.Log;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Delete;
import org.noear.solon.annotation.Get;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Post;
import org.noear.solon.annotation.Put;
import org.noear.solon.validation.annotation.Validated;

/**
 * SysPostController
 *
 * @author leap
 * @date 2024/8/21
 */
@Controller
@Mapping("/system/post")
public class SysPostController {
    @Inject
    SysPostService postService;

    /**
     * 获取岗位列表
     */
    @SaCheckPermission("system:post:list")
    @Get
    @Mapping("/list")
    public TableDataInfo<SysPost> list(SysPost post) {
        return postService.selectPostPage(post);
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @SaCheckPermission("system:post:query")
    @Get
    @Mapping(value = "/{postId}")
    public AjaxResult getInfo(Long postId) {
        return AjaxResult.success(postService.selectPostById(postId));
    }

    /**
     * 新增岗位
     */
    @SaCheckPermission("system:post:add")
    @Post
    @Mapping
    public AjaxResult add(@Validated SysPost post) {
        if (!postService.checkPostNameUnique(post)) {
            return AjaxResult.error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (!postService.checkPostCodeUnique(post)) {
            return AjaxResult.error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setCreateBy(LoginHelper.getUsername());
        return AjaxResult.success(postService.insertPost(post));
    }

    /**
     * 修改岗位
     */
    @SaCheckPermission("system:post:edit")
    @Put
    @Mapping
    public AjaxResult edit(@Validated SysPost post) {
        if (!postService.checkPostNameUnique(post)) {
            return AjaxResult.error("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (!postService.checkPostCodeUnique(post)) {
            return AjaxResult.error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setUpdateBy(LoginHelper.getUsername());
        return AjaxResult.success(postService.updatePost(post));
    }

    /**
     * 删除岗位
     */
    @SaCheckPermission("system:post:remove")
    @Delete
    @Mapping("/{postIds}")
    public AjaxResult remove(Long[] postIds) {
        return AjaxResult.success(postService.deletePostByIds(postIds));
    }
}
